package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysUser;
import cn.wgx.modules.sys.entity.SysUserFavorite;
import cn.wgx.modules.sys.services.SysUserFavoriteService;
import cn.wgx.modules.sys.util.UserUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "2.5 用户关注")
@RestController
@RequestMapping(path = "api/sysUserFollow")
public class SysUserFollowController extends BaseController {

    @Resource
    SysUserFavoriteService service;

    @ApiOperation(value = "已关注人员列表(我关注的人)", notes = "可选参数: user_id(不填获取本人关注列表)")
    @ApiPermission(false)
    @RequestMapping(path = "list", method = RequestMethod.GET)
    public Map<String, Object> getList(
            @RequestParam(required = false) Integer user_id,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
            @ApiParam(value = "是否倒序,默认是") @RequestParam(defaultValue = "true", required = false) Boolean desc
    ) {
        SysUserFavorite record = new SysUserFavorite();
        if (user_id == null) {
            SysUser user = UserUtil.getUser();
            if (user != null) {
                user_id = user.getUser_id();
            }
        }
        record.setUser_id(user_id);
        record.setType(4);
        return returnData(PageHelper.startPage(page, Math.min(Math.abs(pageSize), maxPageSize)), service.getListByType(record, desc, null));
    }

    @ApiOperation(value = "被关注人员列表(粉丝列表)", notes = "可选参数: user_id(不填获取本人的粉丝)")
    @ApiPermission(false)
    @RequestMapping(path = "listFans", method = RequestMethod.GET)
    public Map<String, Object> getListFans(
            @RequestParam(required = false) Integer user_id,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
            @ApiParam(value = "是否倒序,默认是") @RequestParam(defaultValue = "true", required = false) Boolean desc
    ) {
        SysUserFavorite record = new SysUserFavorite();
        if (user_id == null) {
            SysUser user = UserUtil.getUser();
            if (user != null) {
                user_id = user.getUser_id();
            }
        }
        record.setRes_id(user_id);
        record.setType(4);
        return returnData(PageHelper.startPage(page, pageSize), service.getListByType(record, desc, "user_id"));
    }

    @ApiOperation(value = "检测是否关注", notes = "必须参数: user_id")
    @ApiPermission(false)
    @RequestMapping(path = "getCheck", method = RequestMethod.POST)
    public Map<String, Object> checkFollow(@RequestBody List<Integer> ids) {
        Map<String, Object> stringObjectMap = returnData(service.checkHas(ids, 4));
        return stringObjectMap;
    }
//
//
//    @ApiOperation(value = "统计", notes = "获取信息. 必须参数: user_id" )
//    @ApiPermission(false)
//    @RequestMapping(path = "getCount", method = RequestMethod.GET)
//    public Map<String, Object> getCount(@RequestParam Integer user_id) {
//        return returnData(service.getCount(user_id,4));
//    }


    @ApiOperation(value = "添加关注", notes = "必须参数: user_id")
    @ApiPermission(false)
    @RequestMapping(path = "add", method = RequestMethod.POST)
    public Map<String, Object> insertOne(@RequestBody Map record) {
        if (record == null) {
            return returnErrorMap("参数错误");
        }
        Object user_id = record.get("user_id");
        if (!(user_id instanceof Integer)) {
            return returnErrorMap("参数错误2");
        }
        SysUser user = UserUtil.getUser();
        Integer uid = (Integer) user_id;
        if (UserUtil.getUserById(uid) == null) {
            return returnErrorMap("user_id错误");
        }
        if (service.getOne(user.getUser_id(), 4, uid) != null) {
            return returnErrorMap("已关注");
        }
        if (uid.equals(user.getUser_id())) {
            return returnErrorMap("太自恋了吧,还想关注自己?");
        }
        SysUserFavorite record2 = new SysUserFavorite();
        record2.setUser_id(user.getUser_id());
        record2.setType(4);
        record2.setRes_id(uid);
        service.insertOne(record2);
        return returnData(record2.get_result());
    }


    @ApiOperation(value = "取消关注", notes = "必须参数: user_id")
    @ApiPermission(false)
    @RequestMapping(path = "del/{user_id}", method = RequestMethod.DELETE)
    public Map<String, Object> deleteOne(@PathVariable("user_id") Integer res_id) {
        SysUser user = UserUtil.getUser();
        if (user == null) {
            return returnErrorMap("重新登录");
        }
        if (Validation.isNullOrZero(res_id)) {
            return returnErrorMap("参数错误");
        }
        return returnData(service.deleteOne(user.getUser_id(), 4, res_id));
    }


}